package com.lyn.pluginbazaar.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class FileUtil {

    public static String ROOT_PATH = "/pluginRoot";
    public static String PLUGIN_PATH = ROOT_PATH + "/plugin";
    public static String PLUGIN_FILE_PATH = PLUGIN_PATH + "/file";

    public static File createFolder(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public static File createFolder(String path, String folderName) {
        File file = new File(path, folderName);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    public static File createFile(String folderPath, String fileName) {
        try {
            File file = new File(folderPath, fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static InputStream getFileInputStream(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    public static void fileToOutputStream(String fileName, HttpServletResponse response) {
        try {
            File file = new File(PLUGIN_FILE_PATH,fileName);
            if (file.exists()) {
                ServletOutputStream outputStream = response.getOutputStream();
                FileInputStream inputStream = new FileInputStream(file);
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                int length;
                byte[] buffer = new byte[1024];
                while ((length = bufferedInputStream.read(buffer)) != -1) {
                    bufferedOutputStream.write(buffer, 0, length);
                }
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
                outputStream.close();
                bufferedInputStream.close();
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean saveFile(InputStream inputStream, String fileName) {
        try {
            if (inputStream == null) {
                return false;
            }
            if (CheckUtil.isEmpty(fileName)) {
                fileName = UUidUtil.getUid();
            }
            File file = new File(PLUGIN_FILE_PATH, fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream outputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            int length = 0;
            byte[] buffer = new byte[1024];
            while ((length = inputStream.read(buffer)) != -1) {
                bufferedOutputStream.write(buffer, 0, length);
            }
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean delete(String fileName){
        try {
            File file=new File(PLUGIN_FILE_PATH,fileName);
            if (file.exists()){
               return file.delete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
